import DymForm from "./DymForm.vue";
import {createFormModel} from "./form";
// import {buildFormDep} from "@/zform/hooks/build";

/**
 * 创建formCommon mixin
 * @param formDef {{}}
 * @param formMetas {{}}
 */
export let createZformCommon = function ({formDef, formMetas, formTemplate, formPROPID}) {
  // console.log(formTemplate)
  // console.log(formPROPID)
  // console.log(formDef)
  let partName = formDef.name;
  // console.log('createZformCommon')
  return {
    components: {DymForm},
    props: {
      // config: String,
      /**
       * 调试开关
       */
      debug: {
        type: Boolean
      }
    },
    inject: ['zformLoader'],
    data() {
      return {
        formMetas,
        inited: false,
        partName,
        formDef: {
          parts: [
            formDef
          ]
        },
        z_model: {
        }
      }
    },
    mounted() {
      this.inited = true
    },
    beforeDestroy() {
      document.body.querySelector(formTemplate).remove()
      delete globalThis[formPROPID]
      // console.log('destory', formPROPID, formTemplate)
    },
    methods: {
      callEvent(funcName, scope, $event) {
        this[funcName](scope, $event)
      },
      zform__callMethod(funcName, scope, $event) {
        this[funcName](scope, $event)
      },
      不是自定义数组(scope) {
        return !this.zform__objGet(scope.config, "ui.isCustomRender", false)
      },
      zform__onFire([name, e, originEvent] = []) {
        // console.log('zform__onFire', name, e)
        this.zform__callEvent(name, e, originEvent)
      },
      zform__objGet(obj, path, defaultVal) {
        let v =  ZY.lodash.get(obj, path, defaultVal)
        // console.log('zform__objGet', v)
        return v
      },
      zform__callEvent(name, scope, e) {
        // console.log(this.zformLoader)
        this.zformLoader.emit(name, {
          scope,
          e
        })
      },
      zform__reset(scope, e) {
        // console.log(this.zformLoader, scope, e)
        // console.log('reset')
        this.zformLoader.reset(scope, e)
        this.zform__callEvent('form-reset', scope, e)
      },
      private__getTarget(scope) {
        let curIns = this.zformLoader.getCurIns()
        let model = curIns.getModel()
        let s_path = ZY.getObjPathFromPathArr(scope.pathArr)

        return ZY.lodash.get(model, s_path)
      },
      zform__appendArr(scope) {
        // console.log('zform__appendArr', this.arrChange)
        let curIns = this.zformLoader.getCurIns()
        let model = curIns.getModel()
        let s_path = ZY.getObjPathFromPathArr(scope.pathArr)
        if (this.arrChange) {
          this.arrChange()
        }
        let target = ZY.lodash.get(model, s_path)
        if (target) {
          // console.log(target)
          let itemType = scope.config.items.type
          if (itemType === 'object') {
            let obj = createFormModel(scope.config.items);
            // console.log(obj, scope.config.items)
            target.push(obj)
          }
        }

        // console.log(model)
        // console.log(scope.config)
      },
      zform__deleteArr(scope) {
        let target = this.private__getTarget(scope)
        let index = scope.indexKey
        // console.log(scope, target, index)
        // target.splice(index, 1)
        this.$delete(target, index)
        this.$nextTick(async () => {
          let curIns = this.zformLoader.getCurIns()
          let model = await curIns.exportCtx.getRawData()
          // console.log(model)
        })
        if (this.arrChange) {
          this.arrChange('delete', {index})
        }
        // console.log(target, index)
      }
    }
  }
}
